﻿using IdentityModel;
using Microsoft.AspNetCore.Http;
using Service.Core.Redis.Services;
using Service.Framework.Utils;
using System.Net.Http;

namespace Service.Core.Identity.Login
{
    public class LoginUserSession
        : ILoginUserSession
    {
        private readonly IRedisService _redisService;
        private readonly LoginUserInfo _userInfo;
        private readonly IHttpContextAccessor _httpContextAccessor;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="redisService"></param>
        /// <param name="httpContextAccessor"></param>
        public LoginUserSession(IRedisService redisService, IHttpContextAccessor httpContextAccessor)
        {
            _redisService = redisService;
            _httpContextAccessor = httpContextAccessor;
            if (IsAuthenticated)
            {
                _userInfo = BuildLoginUserInfo().GetAwaiter().GetResult();
            }
        }

        public async Task<LoginUserInfo> BuildLoginUserInfo()
        {
            var id = IdentityName;
            if (string.IsNullOrWhiteSpace(id))
            {
                return null;
            }
            var userInfo = await _redisService.Database.StringGetAsync($"User:{id}");
            return JsonSerializeUtil.Deserialize<LoginUserInfo>(userInfo);
        }


        public LoginUserInfo UserInfo => _userInfo;




        public string[] Roles => GetRolesAsync();

        public string ClientId => GetClientIdAsync();



        public string IdentityName => GetIdentityNameAsync();
        public string TenantId => GetTenantIdAsync();

        private bool IsAuthenticated => _httpContextAccessor?.HttpContext?.User.Identity?.IsAuthenticated ?? false;

        private string GetClientIdAsync()
        {
            if (!IsAuthenticated)
                return "";
            return _httpContextAccessor?.HttpContext?.User?.Claims
                .Where(c => c.Type == JwtClaimTypes.ClientId)
                .Select(c => c.Value)
                .FirstOrDefault() ?? "";
        }


        private string[]? GetRolesAsync()
        {
            if (!IsAuthenticated)
                return null;
            return _httpContextAccessor?.HttpContext?.User?.Claims
                .Where(c => c.Type == JwtClaimTypes.Role)
                .Select(c => c.Value)
                .ToArray();
        }


        private string GetIdentityNameAsync()
        {
            if (!IsAuthenticated)
                return "";
            return _httpContextAccessor?.HttpContext?.User?.Claims
                .Where(c => c.Type == JwtClaimTypes.Subject)
                .Select(c => c.Value)
                .FirstOrDefault();
        }

        private string GetTenantIdAsync()
        {
            if (!IsAuthenticated)
                return "";
            return _httpContextAccessor?.HttpContext?.User?.Claims
                .Where(c => c.Type == "tenant_id")
                .Select(c => c.Value)
                .FirstOrDefault() ?? "";
        }



    }
}
